package IO流.验证码;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class ImStr {
    public static void main(String[] args) {

        Random rand=new Random();
        var len=4;
        var w=200;
        var h=100;
        var t=2;
        var font=new Font("宋体",Font.BOLD,rand.nextInt(25,40));
        BufferedImage i = new BufferedImage(w,h,t);
            Graphics2D g = i.createGraphics();
            //设置平滑度
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
            g.setColor(Color.blue);
            g.drawRect(0,0,w,h);
            g.fillRect(0,0,w,h);
            //设置验证码
        String  letter="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        double radianPercent = 0D;
        StringBuffer sbu=new StringBuffer();
        Point[] points = { new Point(0, 0), new Point(10, 50), new Point(30, 6), new Point(60, 55), new Point(80, 3),
                new Point(160, 60)};
        GeneralPath path = new GeneralPath();
        path.moveTo(points[0].x, points[0].y);
        for (int i2 = 0; i2 < points.length - 1; ++i2) {
            Point sp = points[i2];
            Point ep = points[i2 + 1];
            Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
            Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
            path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
        }
        g.setStroke(new BasicStroke(rand.nextInt(6) + 2));
         g.draw(path);

        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
        int yy = rand.nextInt(15) + 15;// y坐标位置
        int hh = rand.nextInt(20) + 10;// 高度
        int aa = rand.nextInt(60) + 20;//频率
        // x
        for (int x = 10; x < w; x++) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            // g.drawLine(x, (int) y, x, (int) y);
            g.fillOval(x, y, 3, 3);
        }

        StringBuilder sok = new StringBuilder(len);
        g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
        for (int n = 0; n < len; n++) {
            font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 35 + 30);
            g.setFont(font);
            int x = n * 32 + 5;
            int y = rand.nextInt(35) + 20;
            radianPercent = Math.PI * (rand.nextInt(35) / 180D);

        }

        for (int j = 0; j < len; j++) {
            //设置字体
            //设置字号
            font = new Font("宋体", Font.BOLD, rand.nextInt(25, 40));
            g.setFont(font);
            var x = 10;
            var y = 20;
            radianPercent = Math.PI * (rand.nextInt(35,165));
            g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)));
            var pos = rand.nextInt(letter.length());
            var s = letter.substring(pos, pos + 1);
            if (rand.nextBoolean()) {
                radianPercent = -radianPercent;

                g.rotate(-radianPercent, x, y);
                g.drawString(s, 10 + j * 30, 50);

            }
        }

        //      关闭画笔
            g.dispose();

        try {
            ImageIO.write(i,"png",new File("i.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //打开图像
            try {
                Runtime.getRuntime().exec("cmd /k start i.png");
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}
